<?php 
namespace Home\Controller;
use \Think\Controller;
//通用的curd控制器
//公共控制器 
class SimpleController extends Controller
{
    public function _initialize(){
         //echo CONTROLLER_NAME ;
         $this->model = D(CONTROLLER_NAME) ;
    }
    public function _empty()
    {
        
        $this->error('无效操作!','',1);
    }

    public function index()
    {
        $this->display();
    }

    // 添加
    public function add()
    {
        if (empty($_SESSION['user'])) {
            $this->error('亲,请先登录!',U('Home/Login/index'));
        }
        if(IS_POST){
            $model = $this->model ;
            if($model->create()){
                   $model->add();
            }else{
                 $this->error( $model->getError() );   
            }
        }else{
            $this->display();
        }
        
    }

    // 更新
    public function update()
    {
        if (empty($_SESSION['user'])) {
            $this->error('亲,请先登录!',U('Home/Login/index'));
        }
        if(IS_POST){
            $model = $this->model ;
            if($model->create()){
                   $model->save();
            }else{
                 $this->error($model->getError());   
            }
        }else{
            $this->display();
        }
    }
    
    // 删除
    public function del()
    {
        if (empty($_SESSION['user'])) {
            $this->error('亲,请先登录!',U('Home/Login/index'));
        }
        $id = I('get.id',0) ;
         if(method_exists( $this->model, 'onDelete')){
               if( $this->model->onDelete($id) ){
                    $this->model->delete($id);
                    $this->success( "删除成功!",U('index') );
               }else{
                    //  ...
                    $this->success( "删除失败!",U('index') );
               }
         }else{
                $this->model->delete($id);
                $this->success( "删除成功!",U('index') );
         }
         
    }

    // 注销登录
    
}